import {
  Controller,
  Post,
  Body,
  Delete,
  Put,
  Patch,
  Param
} from '@nestjs/common'
import { NoticeService } from './notice.service'
import { CreateNoticeDto } from './dto/create-notice.dto'
import { ApiBody, ApiOperation, ApiTags, ApiParam } from '@nestjs/swagger'
import { HttpCode } from '@nestjs/common/decorators'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { HttpStatus } from '@nestjs/common/enums'
import { FResponse } from 'src/utils/reponse'
import { UpdateNoticeDto } from './dto/update-notice.dto'
@NoAuth()
@ApiTags('公告')
@Controller('notice')
export class NoticeController {
  constructor(private readonly noticeService: NoticeService) {}

  @Post()
  @ApiOperation({ summary: '创建公告' })
  @ApiBody({ type: CreateNoticeDto, description: '公告信息' })
  @HttpCode(HttpStatus.OK)
  async create(@Body() createNoticeDto: CreateNoticeDto) {
    return FResponse.success(await this.noticeService.create(createNoticeDto))
  }

  @Post('all')
  @ApiOperation({ summary: '获取所有公告' })
  @ApiBody({
    description: '公告信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
      status?: string
    }
  ) {
    return FResponse.success(
      await this.noticeService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        isDelete: body.isDelete,
        status: body.status
      })
    )
  }

  @Patch(':id')
  @ApiOperation({ summary: '修改指定公告' })
  @ApiParam({ name: 'id', description: '公告ID' })
  @ApiBody({ type: UpdateNoticeDto, description: '公告信息' })
  async update(
    @Param('id') id: string,
    @Body() updateNoticeDto: UpdateNoticeDto
  ) {
    return FResponse.success(
      await this.noticeService.update(+id, updateNoticeDto)
    )
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除公告' })
  @ApiBody({ type: [Number], description: '公告ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.noticeService.remove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复公告' })
  @ApiBody({ type: [Number], description: '公告ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.noticeService.restore(ids))
  }
}
